
/// App 框架顶部导航栏管理类，包括页面跳转回调

import 'package:flutter/material.dart';
import 'package:flutter_navigator/OSNavigator/OSTabBarNavigator.dart';
import 'package:flutter_navigator/Pages/AlarmPage.dart';
import 'package:flutter_navigator/Pages/HomeDetailPage.dart';
import 'package:flutter_navigator/Pages/MinePage.dart';
import 'package:flutter_navigator/Pages/Portal/LoginPage.dart';
import 'package:flutter_navigator/Pages/Portal/RegistPage.dart';

typedef RouteChangeListener(RouteStatusInfo current, RouteStatusInfo? pre);

///创建页面
pageWarp(Widget child) {
  return MaterialPage(key: ValueKey(child.hashCode), child: child);
}

/// 获取routeStatus在页面栈中的位置
int getPageIndex(List<MaterialPage> pages, RouteStatus routeStatus) {
  for (int i = 0; i < pages.length; i++) {
    MaterialPage page = pages[i];
    if (getStatus(page) == routeStatus) {
      return i;
    }
  }
  return -1;
}

/// 自定义路由封装，路由状态
enum RouteStatus { login, regist, home, homeDetail, mine, alarm, unknown }

///获取page对应的RouteStatus
RouteStatus getStatus(MaterialPage page) {
  if (page.child is AMLoginPage) {
    return RouteStatus.login;
  } else if (page.child is OSTabBarNavigator) {
    return RouteStatus.home;
  } else if (page.child is AMHomeDetailPage) {
    return RouteStatus.homeDetail;
  } else if (page.child is AMMinePage) {
    return RouteStatus.mine;
  } else if (page.child is AMRegistPage) {
    return RouteStatus.regist;
  } else if (page.child is AMAlarmPage) {
    return RouteStatus.alarm;
  } else {
    return RouteStatus.unknown;
  }
}

/// 路由信息
class RouteStatusInfo {
  final RouteStatus routeStatus;
  final Widget page;
  RouteStatusInfo(this.routeStatus, this.page);
}

///监听路由页面跳转
///感知当前页面是否压后台

class OSNavigator extends _RouteJumpListener {
  static OSNavigator? _instance;
  RouteJumpListener? _routeJum;
  //缓存所有监听的数组
  List<RouteChangeListener> _listeners = [];
  //记录当前打开的页面
  RouteStatusInfo? _current;
  //首页底部TabBar
  RouteStatusInfo? _tabBar;
  OSNavigator._();
  static OSNavigator getInstance() {
    if (_instance == null) {
      _instance = OSNavigator._();
    }
    return _instance!;
  }

  ///首页底部TabBar切换监听
  void onTabBarChange(int index, Widget page){
    _tabBar = RouteStatusInfo(RouteStatus.home, page);
    if(_tabBar != null){
      _notify(_tabBar!);
    }
  }

  /// 注册路由跳转逻辑
  void registerRouteJump(RouteJumpListener routeJumpListener) {
    this._routeJum = routeJumpListener;
  }

  /// 注册路由跳转逻辑
  void addListener(RouteChangeListener listener) {
    if (!_listeners.contains(listener)) {
      _listeners.add(listener);
    }
  }

  /// 移除监听
  void removeListener(RouteChangeListener listener) {
    _listeners.remove(listener);
  }

  @override
  void onJumpTo(RouteStatus routeStatus, {Map? args}) {
    if (_routeJum != null && _routeJum!.onJumpTo != null) {
      _routeJum!.onJumpTo!(routeStatus, args: args);
    }
  }

  ///通知路由页面变化
  void notify(List<MaterialPage> currentPages, List<MaterialPage> prePages) {
    if (currentPages == prePages) return;
    //当前页面信息
    var current =
        RouteStatusInfo(getStatus(currentPages.last), currentPages.last.child);
    _notify(current);
  }

  void _notify(RouteStatusInfo current) {
    if(current.page is OSTabBarNavigator && _tabBar != null){
      _current = _tabBar;
    }

    print('OSNavigator:current:${current.page}');
    print('OSNavigator:pre:${_current?.page}');
    _listeners.forEach((listener) {
      //此时_current未更新，则为上次打开的页面
      listener(current, _current);
    });
    // 记录当前打开的页面
    _current = current;
  }
}

/// 抽象类供OSNavigator实现
abstract class _RouteJumpListener {
  void onJumpTo(RouteStatus routeStatus, {Map? args});
}

typedef OnJumpTo = void Function(RouteStatus routeStatus, {Map? args});

/// 定义路由跳转逻辑要实现的功能
class RouteJumpListener {
  final OnJumpTo? onJumpTo;

  RouteJumpListener({this.onJumpTo});
}
